import { getCode, http } from "../../utils/index";

Page({
  async handleLogin() {
    // 1. 获取 code
    const code = await getCode();
    console.log(code);
    // 2. 发送网络请求
    const resp = await http({
      url: "http://localhost:3000/loginByWx",
      method: "POST",
      data: { code },
    });
    console.log(resp);
    // 3. 后续
  },

  async handleLogin2(event) {
    // 1. 解构出加密数据
    const { encryptedData, iv } = event.detail;
    // 2. 获取 code
    const code = await getCode();
    // 3. 调用后端接口
    const resp = await http({
      url: "http://localhost:3000/loginByWx2",
      method: "POST",
      data: { code, encryptedData, iv },
    });
    console.log(resp);
  },

  // wx.getUserInfo()
  fn1() {
    wx.getUserInfo({
      success: (res) => {
        console.log(res);
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },

  //   wx.getUserProfile(
  fn2() {
    wx.getUserProfile({
      desc: "我们需要",
      success: (res) => {
        console.log("success", res);
      },
      fail: () => {
        console.log("fail");
      },
    });
  },

  fn3(event) {
    console.log("event", event);
    // event.detail.avatarUrl 就是头像地址，地址格式如下：
    // http://tmp/O1k0HgIdnh9q18eb23211f0866f9190c4db06bcde7fd.jpeg
    // 注意的是，该地址是一个临时地址，需要调用 wx.uploadFile() 将该地址上传到
    //  开发者自己的服务器上才行。

    // 文件上传
    wx.uploadFile({
      filePath: event.detail.avatarUrl,
      name: "file",
      url: "http://localhost:3000/upload",
      success: (res) => {
        console.log("文件上传成功", res);
      },
    });
  },

  fn4() {
    wx.chooseImage({
      // 数量
      count: 1,
      //   图片大小 original - 原图  compressed - 压缩
      sizeType: ["original", "compressed"],
      //   图片来源 album - 相册  camera - 相机
      sourceType: ["album", "camera"],
      success: (res) => {
        console.log("res", res);
      },
    });
  },

  fn5() {
      wx.requestSubscribeMessage({
        tmplIds: ["X8m17h2BWcvrl_oyvu0XqGT67jolMb6nqq2sX8zG85g"],
      })
  }
});
